<?

print "<form class=\"sform\">";
print "<div class=\"d\">";
print "View style<br/>";
print "<select name=\"view\" onchange=\"this.form.submit()\">";
foreach ($views as $key => $value) {
	printf("<option value=\"%s\"%s>%s</option>",$key,($key==$opt['view'])?' selected':'',$value);
}
print "</select></div>";

print "<div class=\"d\">";
if (!empty($_GET['opt'])) {
	print "<b>Within Group</b> ";
}
print "Order<br/>";
print "<select name=\"order\" onchange=\"this.form.submit()\">";
foreach ($orders as $key => $value) {
	printf("<option value=\"%s\"%s>%s</option>",$key,($key==$opt['order'])?' selected':'',$value);
}
print "</select>";
print "<select name=\"in\" onchange=\"this.form.submit()\">";
$ins = array('a'=>'Asc','d'=>'Desc');
foreach ($ins as $key => $value) {
	printf("<option value=\"%s\"%s>%s</option>",$key,($key==$opt['in'])?' selected':'',$value);
}
print "</select></div>";

print "<div class=\"d\">";
print "Option<br/>";
print "<select name=\"opt\" onchange=\"this.form.submit()\">";
foreach ($options as $key => $value) {
	printf("<option value=\"%s\"%s>%s</option>",$key,($key==$opt['opt'])?' selected':'',$value);
}
print "</select></div>";

print "<br>";

if (isset($_GET['slider']) || !empty($_GET['taken_start']) || !empty($_GET['taken_end']) ) {

	if (!empty($_GET['taken']) && preg_match('/^\d{4}$/',$_GET['taken'])) {
		$dates = array('min'=>$_GET['taken'],'max'=>$_GET['taken']);
	} else {
		$dates = getRow("SELECT SUBSTRING(MIN(imagetaken),1,4) AS min,SUBSTRING(MAX(imagetaken),1,4) AS max FROM p2.gridimage{$portal_id}_bi_view WHERE imagetaken > 1");
	}

	$isshort = (abs($dates['min']-$dates['max']) < 30);

	if (!empty($_GET['taken_start'])) {
		$start = $_GET['taken_start'];
	} elseif ($isshort) {
		$start = $dates['min']."/01";
	} else {
		$start = $dates['min'];
	}
	if (!empty($_GET['taken_end'])) {
		$end = $_GET['taken_end'];
	} elseif ($isshort) {
		$end = $dates['max']."/12";
	} else {
		$end = $dates['max'];
	}

	print "<div style=\"font-size:0.7em;margin-left:10px;margin-right:10px\">";
	print "<big>Date Range</big> &nbsp; <input type=\"submit\" value=\"Update\"> <span style=\"color:silver\">&lt;- Click this when changed the date slider below (<a href=\"/breakdown.php?by=year\">View available year breakdown</a>)</span>";
	print "<select name=\"taken_start\" id=\"taken_start\" style=\"display:none\">";
	if ($isshort) {

		foreach (range($dates['min'],$dates['max']) as $year) {
			print "<optgroup label=\"$year\">";
			foreach (range(1,12) as $month) {
				$ym = sprintf("%02d/%02d",$year,$month);
				printf("<option value=\"%s\"%s>%04d-%02d</option>",$ym,($ym==$start)?' selected':'',$year,$month);
			}
			print "</optgroup>";
		}
	} else {
		foreach (range($dates['min'],$dates['max']) as $year) {
			printf("<option value=\"%s\"%s>%04d</option>",$year,($year==$start)?' selected':'',$year);
		}
	}
	print "</select>";

	print "<select name=\"taken_end\" id=\"taken_end\" style=\"display:none\">";
	if ($isshort) {
		foreach (range($dates['min'],$dates['max']) as $year) {
			print "<optgroup label=\"$year\">";
			foreach (range(1,12) as $month) {
				$ym = sprintf("%02d/%02d",$year,$month);
				printf("<option value=\"%s\"%s>%04d-%02d</option>",$ym,($ym==$end)?' selected':'',$year,$month);
			}
			print "</optgroup>";
		}
	} else {
		foreach (range($dates['min'],$dates['max']) as $year) {
			printf("<option value=\"%s\"%s>%04d</option>",$year,($year==$end)?' selected':'',$year);
		}
	}
	print "</select></div>";

	?>

		<script type="text/javascript" src="slider/js/jquery-1.3.2.min.js"></script>
		<script type="text/javascript" src="slider/js/jquery-ui-1.7.1.custom.min.js"></script>
		<script type="text/javascript" src="slider/js/selectToUISlider.jQuery.js"></script>
		<link rel="stylesheet" href="slider/css/redmond/jquery-ui-1.7.1.custom.css" type="text/css" />
		<link rel="Stylesheet" href="slider/css/ui.slider.extras.css" type="text/css" />
		<style type="text/css">
			.ui-slider {clear: both; }
		</style>
		<script type="text/javascript">
			$(function(){


				$('select#taken_start, select#taken_end').selectToUISlider({
					labels: <? echo (($isshort)?1:10); ?>
				});


			});

		</script>

	<?

	print "<br>";
	print "<br>";
	print "<br>";
	print "<br>";


} else {
	$dates = getRow("SELECT SUBSTRING(MIN(imagetaken),1,4) AS min,SUBSTRING(MAX(imagetaken),1,4) AS max FROM p2.gridimage{$portal_id}_bi_view WHERE imagetaken > 1");
	
	
	if ($dates['min'] != $dates['max']) {
		$link = linktoself(array('slider'=>1));	
		print "&middot; <a href=\"$link\">Enable Date Range slider</a> <span style=color:gray>(Not tested in all browsers yet)</span>";
	}
}


include($_SERVER['DOCUMENT_ROOT'].'/filter_setup.inc.php');

print "</form>";

